// 实现这个项目的构建任务
const mozjpeg = require('imagemin-mozjpeg')
const browserSync = require('browser-sync')
const bs = browserSync.create()

// 数据
const data = {
  menus : [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

module.exports = function (grunt) {
  require('load-grunt-tasks')(grunt)
  grunt.initConfig({
    // 编译sass
    sass: {
      options: {
        style: 'expanded'
      },
      main: {
        files: [{
          expand: true,
          cwd: 'src/',
          ext: '.css',
          extDot: 'first',
          filter: 'isFile',
          src: ['**/*.{scss, sass}'],
          dest: 'temp/'
        }]
      }
    },
    // 编译js
    babel: {
      options: {
        sourceMap: false,
        presets: ['@babel/preset-env']
      },
      main: {
        files: [{
          expand: true,
          cwd: 'src/',
          filter: 'isFile',
          src: ['**/*.js'],
          dest: 'temp/'
        }]
      }
    },
    // 编译模版文件
    web_swig: {
      options: {
        swigOptions:{
          cache: false
        },
        getData: () => {
          return data
        }
      },
      main: {
        files: [{
          expand: true,
          cwd: 'src/',
          filter: 'isFile',
          src: ['**/*.html'],
          dest: 'temp/'
        }]
      }
    },
    // 其他文件的复制
    copy: {
      extra: {
        files: [{
          expand: true,
          cwd: 'public/',
          filter: 'isFile',
          src: ['*.*'],
          dest: 'dist/'
        }]
      }
    },
    // 图片、字体文件的压缩
    imagemin: {
      static: {
        options: {
          optimizationLevel: 3,
          svgoPlugins: [{removeViewBox: false}],
          use: [mozjpeg()] // Example plugin usage
        },
        files: [{
          expand: true,
          cwd: 'src/',
          src: ['**/*.{png,jpg,jepg,gif,svg,eot,ttf,woff}'],
          dest: 'dist/'
        }]
      },
      dynamic: {
        files: [{
          expand: true,
          cwd: 'src/',
          src: ['**/*.{png,jpg,jepg,gif,svg,eot,ttf,woff}'],
          dest: 'dist/'
        }]
      }
    },
    // 启动服务阶段动态监听文件变化
    watch: {
      styles: {
        files: ['src/**/*.scss'],
        tasks: ['styles']
      },
      scripts: {
        files: ['src/**/*.js'],
        tasks: ['scripts']
      },
      pages: {
        files: ['src/**/*.html'],
        tasks: ['pages']
      },
      static: {
        files: ['src/**/*.{png,jpg,jepg,gif,svg,eot,ttf,woff}', 'public/*.*'],
        tasks: ['bsReload']
      }
    },
    // css文件的压缩
    cssmin: {
      options: {
        mergeIntoShorthands: false,
        roundingPrecision: -1
      },
      main: {
        files: [{
          expand: true,
          cwd: 'temp/',
          src: ['**/*.css'],
          dest: 'dist/'
        }]
      }
    },
    // html文件的压缩
    htmlmin: {
      options: {
        removeComments: true,
        collapseWhitespace: true
      },
      main: {
        files: [{
          expand: true,
          cwd: 'temp/',
          src: ['**/*.html'],
          dest: 'dist/'
        }]
      }
    },
    // js文件压缩
    uglify: {
      main: {
        files: [{
          expand: true,
          cwd: 'temp/',
          src: ['**/*.js'],
          dest: 'dist/'
        }]
      }
    },
    // 清除目录
    clean: {
      all: ['dist', 'temp']
    }
  })

  // 创建样式文件的读取、转换、写入
  grunt.registerTask('styles', function () {
    let done = this.async()
    grunt.task.run('sass')
    done()
  })

  // 创建script文件的读取、转换、写入
  grunt.registerTask('scripts', function () {
    let done = this.async()
    grunt.task.run('babel')
    done()
  })

  // 创建模版文件的读取、转换、写入
  grunt.registerTask('pages', function () {
    let done = this.async()
    grunt.task.run('web_swig')
    done()
  })

  // 其他文件的复制、写入
  grunt.registerTask('extra', function () {
    let done = this.async()
    grunt.task.run('copy')
    done()
  })

  // 图片、字体文件的压缩、写入
  grunt.registerTask('static', function () {
    let done = this.async()
    grunt.task.run('imagemin')
    done()
  })

  // 本地服务的热更新
  grunt.registerTask('bsReload', () => {
    bs.reload()
  })

  // 启用开发阶段使用的本地任务
  grunt.registerTask('bsServe', () => {
    grunt.task.run('watch')
    bs.init({
      files: 'dist/**',
      notify: false,
      server: {
        baseDir: ['temp', 'src', 'publish'],
        routes: {
          '/node_modules': 'node_modules'
        }
      }
    })
  })

  // 编译文件的压缩处理
  grunt.registerTask('useref', function () {
    let done = this.async()
    grunt.task.run('htmlmin')
    grunt.task.run('cssmin')
    grunt.task.run('uglify')
    done()
  })

  // 并行组合需要编译的任务
  grunt.registerTask('compile', ['styles', 'scripts', 'pages'])

  // 组合部署阶段的打包操作任务
  grunt.registerTask('build', ['clean', 'compile', 'useref', 'static', 'copy'])

  // 组合先编译在启用开发服务的任务
  grunt.registerTask('serve', ['compile', 'bsServe'])
}